﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace Alfa.Common.ValueConverters {
	/// <summary>
	/// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
	/// <see cref="Visibility.Collapsed"/>.
	/// </summary>
	public sealed class BooleanToVisibilityConverter : IValueConverter {
		/// <summary>
		/// Converts true to <see cref="Visibility.Visible"/> and false to
		/// <see cref="Visibility.Collapsed"/>.
		/// </summary>
		/// <param name="value">Value to convert. Should be bool.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (<see cref="Visibility"/>)</returns>
		public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
			return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
		}

		/// <summary>
		/// Converts back <see cref="Visibility.Visible"/> to true and 
		/// <see cref="Visibility.Collapsed"/> and <see cref="Visibility.Hidden"/> to false
		/// </summary>
		/// <param name="value">Value to convert. Should be <see cref="Visibility"/>.</param>
		/// <param name="targetType"></param>
		/// <param name="parameter"></param>
		/// <param name="culture"></param>
		/// <returns>Converted value (bool)</returns>
		public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
			return value is Visibility && (Visibility)value == Visibility.Visible;
		}
	}
}
